let changeIconBtn = document.getElementsByClassName('change-icon')[0];
let username = document.getElementsByClassName('username')[0],
    emailInput = document.getElementsByName('email-show')[0],
    phoneInput = document.getElementsByName('phone-show')[0],
    ageInput = document.getElementsByName('age-show')[0],
    phoneBtn = document.getElementsByName('phone-btn')[0],
    emailBtn = document.getElementsByName('email-btn')[0],
    ageBtn = document.getElementsByName('age-btn')[0],
    userIcon = document.getElementsByClassName('user-icon')[0],
    maleSex = document.getElementsByName('sex')[0],
    femaleSex = document.getElementsByName('sex')[1],
    usernameInput = document.getElementsByName('username-show')[0],
    usernameBtn = document.getElementsByName('username-btn')[0];
function iniProfile() {
  isLogin();
  let userIcon = document.getElementsByClassName('user-icon')[0];
  if(sessionStorage.getItem('headImg') == 'null') {
    userIcon.src = path + 'img/user-icon.png';
  }else {
    userIcon.src = 'http://' + sessionStorage.getItem('headImg');
  }
  username.innerHTML = sessionStorage.getItem('username');
  emailInput.value = sessionStorage.getItem('email');
  usernameInput.value = sessionStorage.getItem('username');
  if(sessionStorage.getItem('phone') == 'null') {
    phoneInput.value = ''
    phoneInput.removeAttribute('disabled')
    phoneBtn.innerText = '设置手机号'
  }else  {
    phoneInput.value = sessionStorage.getItem('phone');
  }
  if(sessionStorage.getItem('age') == 'null') {
    ageInput.value = ''
    ageInput.removeAttribute('disabled');
    ageBtn.innerText = '设置年龄'
  }else  {
    ageInput.value = sessionStorage.getItem('age');
  }
  if(sessionStorage.getItem('sex') == '男') {
    maleSex.checked = true;
  }else if(sessionStorage.getItem('sex') == '女') {
    femaleSex.checked = true;
  }
  loadHeadImg();
}
changeIconBtn.onchange = function() {
  let files = document.getElementsByName('icon')[0].files;
  console.log(files[0]);
  let formData = new FormData;
  formData.append('username',sessionStorage.getItem('username'))
  formData.append('headImg',files[0]);
  request('http://175.178.51.126:8091/smallA/uploadHeadImg','POST',formData, 'none')
    .then(value => {
      console.log(value);
      if(value.code == 200) {
        sessionStorage.setItem('headImg', value.data);
        location.reload();
      }
    }).catch(error => {
      console.log(error);
    })
}
maleSex.onchange = function() {
  if(sessionStorage.getItem('sex') != '男') {
    const sexData = {
      id: sessionStorage.getItem('id'),
      sex: '男'
    }
    request('http://175.178.51.126:8091/smallA/updateUserData','POST',JSON.stringify(sexData))
      .then(value => {
        console.log(value);
        if(value.code == 200) {
          sessionStorage.setItem('sex','男');
        }
      }).catch(error => {
        console.log(error);
      })
  }
}
femaleSex.onchange = function() {
  if(sessionStorage.getItem('sex') != '女') {
    const sexData = {
      id: sessionStorage.getItem('id'),
      sex: '女'
    }
    request('http://175.178.51.126:8091/smallA/updateUserData','POST',JSON.stringify(sexData))
      .then(value => {
        console.log(value);
        if(value.code == 200) {
          sessionStorage.setItem('sex','女');
        }
      }).catch(error => {
        console.log(error);
      })
  }
}
/* 保存信息 */
function setInfo(item) {
  console.log(item)
  if(item == 'email') {
    console.log(emailBtn.innerText);
    if(emailBtn.innerText == '更改邮箱') {
      emailInput.removeAttribute('disabled');
      emailBtn.innerText = '确认更改'
    }else if(emailBtn.innerText == '确认更改') {
      const emailData = {
        id: sessionStorage.getItem('id'),
        email: document.getElementsByName('email-show')[0].value
      }
      if(emailData.email == '') {
        alert('邮箱不能为空！');
        location.reload();
        return false
      }
      request('http://175.178.51.126:8091/smallA/updateUserData', 'POST', JSON.stringify(emailData))
        .then(value => {
          console.log(value);
          if(value.code == 200) {
            sessionStorage.setItem('email',value.data.email);
            emailInput.value = value.data.email;
            emailInput.setAttribute('disabled','disabled');
            emailBtn.innerText = '更改邮箱';
          }else {
            alert(value.msg);
            location.reload();
          }
        }).catch(error => {
          console.log(error);
        })
    }
  }
  if(item == 'phone') {
    console.log(phoneBtn.innerText);
    if(phoneBtn.innerText == '更改手机号') {
      phoneInput.removeAttribute('disabled');
      phoneBtn.innerText = '确认更改'
    }else if(phoneBtn.innerText == '确认更改' || phoneBtn.innerText == '设置手机号') {
      const phoneData = {
        id: sessionStorage.getItem('id'),
        phone: document.getElementsByName('phone-show')[0].value
      }
      if(phoneData.phone == '') {
        alert('手机号不能为空！');
        location.reload();
        return false
      }
      request('http://175.178.51.126:8091/smallA/updateUserData', 'POST', JSON.stringify(phoneData))
        .then(value => {
          console.log(value);
          if(value.code == 200) {
            sessionStorage.setItem('phone',value.data.phone);
            phoneInput.value = value.data.phone;
            phoneInput.setAttribute('disabled','disabled');
            phoneBtn.innerText = '更改手机号';
          }else {
            alert(value.msg);
            location.reload();
          }
        }).catch(error => {
          console.log(error);
        })
    }
  }
  if(item == 'username') {
    console.log(usernameBtn.innerText);
    if(usernameBtn.innerText == '更改用户名') {
      usernameInput.removeAttribute('disabled');
      usernameBtn.innerText = '确认更改'
    }else if(usernameBtn.innerText == '确认更改') {
      const usernameData = {
        id: sessionStorage.getItem('id'),
        username: document.getElementsByName('username-show')[0].value
      }
      request('http://175.178.51.126:8091/smallA/updateUserData', 'POST', JSON.stringify(usernameData))
        .then(value => {
          console.log(value);
          if(usernameData.username == '') {
            alert('用户名不能为空！');
            location.reload();
            return false
          }
          if(value.code == 200) {
            sessionStorage.setItem('username',value.data.username);
            usernameInput.value = value.data.username;
            username.innerHTML = sessionStorage.getItem('username');
            usernameInput.setAttribute('disabled','disabled');
            usernameBtn.innerText = '更改用户名';
          }else {
            alert(value.msg);
            location.reload();
          }
        }).catch(error => {
          console.log(error);
        })
    }
  }
  if(item == 'age') {
    console.log(ageBtn.innerText);
    if(ageBtn.innerText == '更改年龄') {
      ageInput.removeAttribute('disabled');
      ageBtn.innerText = '确认更改'
    }else if(ageBtn.innerText == '确认更改' || ageBtn.innerText == '设置年龄') {
      const ageData = {
        id: sessionStorage.getItem('id'),
        age: document.getElementsByName('age-show')[0].value
      }
      if(ageData.age == '') {
        alert('年龄不能为空！');
        location.reload();
        return false
      }
      request('http://175.178.51.126:8091/smallA/updateUserData', 'POST', JSON.stringify(ageData))
        .then(value => {
          console.log(value);
          if(value.code == 200) {
            sessionStorage.setItem('age',value.data.age);
            ageInput.value = value.data.age;
            ageInput.setAttribute('disabled','disabled');
            ageBtn.innerText = '更改年龄';
          }else {
            alert(value.msg);
            location.reload();
          }
        }).catch(error => {
          console.log(error);
        })
    }
  }
}
function changePasswordPage(operation) {
  if(operation == 'open') {
    let layerDiv = document.createElement('div');
    layerDiv.className = 'layer';
    layerDiv.innerHTML = `
    <div class="forget-page">
        <div class="title">
          <h2></h2>
          <h2 onclick="changePasswordPage('close')" id="close">X</h2>
        </div>
        <form name="forgetPass" method="post" class="form">
          <input type="password" name="oldPassword" value="" placeholder="原来的密码"/>
          <input type="password" name="password" value="" placeholder="新的密码"/>
          <input type="password" name="passwordConfirm" value="" placeholder="再次输入新的密码"/>
          <button type="button" name="submit" onclick="changePassword()" id="reset-password" class="primary full">修改密码</button>
        </form>
      </div>
    `
    document.body.appendChild(layerDiv);
  }else if(operation == 'close') {
    let layerDiv = document.getElementsByClassName('layer')[0];
    layerDiv.remove();
  }
}
function changePassword() {
  const passwordData = {
    username: sessionStorage.getItem('username'),
    oldPassword: forgetPass.oldPassword.value,
    newPassword: forgetPass.password.value
  }
  if(passwordData.oldPassword == '' || passwordData.newPassword == '') {
    alert('密码不能为空！');
    return false
  }
  if(passwordData.newPassword != forgetPass.passwordConfirm.value) {
    alert('两次密码输入不一致！');
    return false
  }
  request('http://175.178.51.126:8091/smallA/updatePwd','POST',JSON.stringify(passwordData))
    .then(value => {
      if(value.code == 200) {
        alert('修改成功！');
        changePasswordPage('close');
      }else {
        alert(value.msg)
      }
    }).catch(error => {
      console.log(error);
    })
}
function changePostPage(operation) {
  if(!isGettingAllNews && !isGettingAllTopNews) {
    let body = document.getElementsByTagName('body')[0];
    if(operation == 'open') {
     /*  page.style.display = 'block'; */
     body.innerHTML += `
      <div class="layer">
        <div class="create-news">
          <div class="close" onclick="changePostPage('close')">X</div>
          <div class="create-news-main">
            <div class="user-icon">
              <div class="icon-replace"></div>
            </div>
            <div class="form">
              <form method="post" name="post-news">
                <input type="text" name="title" value="" placeholder="动态标题"/>
                <textarea name="content" placeholder="动态内容"></textarea>
                <div class="operation">
                  <input type="file" name="img" multiple onchange="showFileInfo()"/>
                  <button type="button" class="primary large" onclick="postNews()">发布</button>
                </div>
              </form>
            </div>
          </div>
        </div>
      </div>
     `
    }else if(operation == 'close') {
      let layer = document.getElementsByClassName('layer')[0];
      body.removeChild(layer);
    }
  }
}
